package org.szy.plugin.sys.controller;

import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.ModelAndView;
import org.noear.solon.validation.annotation.NotNull;
import org.noear.solon.validation.annotation.Validated;
import org.szy.core.common.controller.BaseController;
import org.szy.core.common.entity.R;
import org.szy.plugin.sys.entity.SysRoleMenu;
import org.szy.plugin.sys.service.SysRoleMenuService;

/**
 * 角色菜单关联表
 *
 * @author 孙志阳
 * @date 2023-06-26
 * @since 1.0.0
 **/
@Controller
@Mapping("/sys/roleMenu")
public class SysRoleMenuController extends BaseController<SysRoleMenu> {

    private final String PATH_PREFIX = "sys/roleMenu/";
    @Inject
    private SysRoleMenuService sysRoleMenuService;

    @Get
    @Mapping("/grantMenu")
    public ModelAndView grantMenu() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "grantMenu");
        return view;
    }


    /**
     * 获取拥有的菜单
     *
     * @param roleId 角色标识
     */
    @Get
    @Mapping("/ownMenu")
    public R<?> ownMenu(@NotNull Long roleId) {
        return R.data(sysRoleMenuService.listByRoleId(roleId));
    }

    /**
     * 保存角色和菜单关联
     *
     * @param entity 对象
     */
    @Post
    @Mapping("/saveRoleMenu")
    public R<?> saveRoleMenu(@Validated SysRoleMenu entity) {
        boolean save = sysRoleMenuService.add(entity);
        return R.bool(save);
    }
}
